/**
 * @file
 * @auth jinguangguo
 * @date 2018/1/25
 */

import Vue from 'vue';
import VueI18n from 'vue-i18n';
import messages from '../module/market/lang';

import './base/init';

import commonService from '../common/commonService';
import lib from '../common/lib';
import symbolConfig from '../common/symbolConfig';

import MarketInfo from '../component/MarketInfo.vue';
import MarketCoin from '../component/MarketCoin.vue';
import MenuCoin from '../component/Menu/main';
import MenuPopover from '../component/MenuPopover.vue';
import LangPopover from '../component/LangPopover.vue';
import Market from '../module/market/main';
import parseUrl from "../../grsc-base/util/parseUrl";

Vue.use(VueI18n);

let i18n =  new VueI18n({
    locale: window.app.LANG,
    messages, // set locale messages
});

commonService.fetchSymbols((symbolMap, symbolList) => {

    Vue.prototype.$$coinSymbols = symbolMap;

    let symbol = getQuerySymbol();
    updateTitle(symbol);

    let currentSymbol = symbolMap[symbol];

    Vue.prototype.$$tradeType = currentSymbol.assetCode2;
    Vue.prototype.$$tradeUnit = currentSymbol.assetCode1;
    Vue.prototype.$$symbol = currentSymbol.symbol;
    Vue.prototype.$$coinPrecision = currentSymbol[symbolConfig.PRICEPRECISION];
    Vue.prototype.$$highlightNo = currentSymbol[symbolConfig.HIGHLIGHTNO];
    Vue.prototype.$$amountPrecision = currentSymbol[symbolConfig.AMOUNTPRECISION];


    const app = new Vue({
        i18n,
        data() {
            return {
                symbol: symbol
            }
        },
        components: {
            Market,
            MarketInfo,
            MarketCoin,
            MenuPopover,
            LangPopover,
            MenuCoin
        }
    });

    app.$mount('#app');


    function getQuerySymbol() {
        const SYMBOL_DEFAULT = 'BTC_ETH';
        let query = parseUrl();
        let symbol = SYMBOL_DEFAULT;
        if (query.s) {
            symbol = query.s;

            // 判断格式是否正确
            let reg = /^[a-zA-Z]+_[a-zA-Z]+$/;
            if (reg.test(symbol) === true) {
                symbol = lib.revertCoin(symbol);
            } else {
                symbol = SYMBOL_DEFAULT;
            }

            symbol = symbol.toUpperCase();
            if (!symbolMap[symbol]) {
                symbol = SYMBOL_DEFAULT;
            }
        }
        console.log('THE current symbol is ' + symbol);
        return symbol;
    }

    function updateTitle(symbol) {
        let symbolLabl = lib.getSymbolLabel(symbol);
        document.title = symbolLabl + ' - YourDax';
    }
});






